package com.delong.spring.handler.app;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.delong.cache.AppCache;
import com.delong.model.app.App;
import com.delong.model.app.Category;
import com.delong.service.AppService;
import com.delong.service.CategoryService;
import com.delong.spring.handler.BaseController;

@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController {
	Logger log = Logger.getLogger(CategoryController.class);
	CategoryService service = new CategoryService();
	AppService appService = new AppService();

	@RequestMapping("/list")
	public String getAppCategoryList(Map<String, Object> model, ServletRequest req) {
		// HttpServletRequest request = (HttpServletRequest) req;
		model.put("username", this.getUser().getUsername());
		return "app/Category";
	}

	@RequestMapping("/getall")
	@ResponseBody
	public String getAllElement(Map<String, Object> model, ServletRequest req) {
		CategoryService categoryService = new CategoryService();
		List<Category> categorys = categoryService.getListElement();
		JSONObject result = new JSONObject();
		result.put("rows", categorys);
		result.put("code", 0);
		return result.toJSONString();
	}

	@RequestMapping("/getdata")
	@ResponseBody
	public String getElementData(@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "page", defaultValue = "1") int page,
			@RequestParam(value = "rows", defaultValue = "15") int rows) {
		if (!title.trim().equals("")) {
			title = "%" + title + "%";
		}
		List<Category> elements = service.selectCategoryByTitle(title, (page - 1) * rows, rows);
		JSONObject result = new JSONObject();
		result.put("rows", elements);
		int total = this.service.selectCount(title);
		result.put("total", total);
		return result.toJSONString();
	}

	@RequestMapping(value = "/add", method = RequestMethod.POST)
	@ResponseBody
	public String getElementData(@RequestParam(value = "title", defaultValue = "") String title,
			@RequestParam(value = "order", defaultValue = "1") int order,
			@RequestParam(value = "icon", defaultValue = "") String icon,
			@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "type", defaultValue = "0") int type, ServletRequest req) {
		HttpServletRequest request = (HttpServletRequest) req;
		String userName = request.getSession().getAttribute("username").toString();
		Category element = new Category();
		element.setIcon(icon);
		element.setSort(order);
		element.setTitle(title);
		element.setId(id);
		element.setType(type);
		if (id == 0) {
			this.service.addElement(element);
			log.info(userName + "增加分类了id为:" + String.valueOf(element.getId()));
		} else {

			List<App> appElements = this.appService.getListByCategoryId(id);
			AppCache cache = new AppCache();
			cache.deleteApps(appElements);
			this.service.changeElement(element);
			log.info(userName + "修改分类了id为:" + String.valueOf(element.getId()));
		}
		return "{'code':0,'msg':'success'}";
	}

	@RequestMapping(value = "/changestatus")
	@ResponseBody
	public String updateStatus(@RequestParam(value = "id", defaultValue = "0") int id,
			@RequestParam(value = "status", defaultValue = "0") int status, ServletRequest req) {
		HttpServletRequest request = (HttpServletRequest) req;
		String userName = request.getSession().getAttribute("username").toString();
		log.info(userName + "修改分类状态了id为:" + String.valueOf(id) + "状态为" + String.valueOf(status));
		this.service.changeStatus(id, status);
		if (status == 1) {
			List<App> appElements = this.appService.getListByCategoryId(id);
			AppCache cache = new AppCache();
			cache.deleteApps(appElements);
		}
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("msg", "success");
		return result.toJSONString();
	}

	@RequestMapping(value = "/getelement")
	@ResponseBody
	public String getElement(@RequestParam(value = "id", defaultValue = "0") int id) {
		Category element = this.service.selectCategoryById(id);
		JSONObject result = new JSONObject();
		result.put("code", 0);
		result.put("data", element);
		return result.toJSONString();
	}
}
